<!-- subpkg_consult/description/description -->
<script lang="ts" setup>
  import { uploadImageApi } from '@/apis/description'
  import { useConsultStore } from '@/store/consult'
  import type { UploadImage } from '@/types/description'
  import { onLoad, onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'
  const store = useConsultStore()
  const illnessInfo = ref<{
    illnessDesc: string
    illnessTime: number
    consultFlag: number
    pictures: UploadImage[]
  }>({
    illnessDesc: '',
    illnessTime: 1,
    consultFlag: 1,
    pictures: [] as UploadImage[],
  })

  const illnessTimes = [
    { value: 1, text: '一周内' },
    { value: 2, text: '一月内' },
    { value: 3, text: '半年内' },
    { value: 4, text: '半年以上' },
  ]

  // 是否就诊过
  const consultFlags = [
    { value: 1, text: '就诊过' },
    { value: 0, text: '没有就诊过' },
  ]

  // 选择患病时长
  function onTimeTagClick(value: number) {
    // 记录用户选择的数据
    illnessInfo.value.illnessTime = value
  }

  function onFlagTagClick(value: number) {
    // 记录用户选择的数据
    illnessInfo.value.consultFlag = value
  }

  const onSelect = async (e: UniHelper.UniFilePickerOnSelectEvent) => {
    console.log(e.tempFilePaths[0])
    const img = await uploadImageApi(e.tempFilePaths[0])
    illnessInfo.value.pictures.push(img)
  }
  const props = defineProps<{ type: string; illnessType: string; depId: string }>()

  // 点击下一步
  const onNextStepClick = () => {
    store.illnessInfo = illnessInfo.value
    store.depId = props.depId
    store.illnessType = props.illnessType
    store.type = props.type
    uni.navigateTo({
      url: `/subpkg_archive/list/list?type=consult`,
    })
  }

  onLoad(() => {
    // 提示用户是否恢复之前填写的病情数据
    if (store.illnessInfo.illnessDesc) {
      uni.showModal({
        title: '温馨提示',
        content: '是否恢复之前填写的病情信息？',
        confirmText: '确认',
        confirmColor: '#22D66E',
        cancelColor: '#848484',
        success({ confirm }) {
          if (confirm) {
            illnessInfo.value = store.illnessInfo
          } else {
            // 清空缓存数据
            store.illnessInfo = {
              illnessDesc: '',
              illnessTime: 1,
              consultFlag: 1,
              pictures: [],
            }
          }
        },
      })
    }
  })
</script>

<template>
  <scroll-view>
    <view class="description-page">
      <!-- 在线医生 -->
      <view class="doctor-online">
        <image src="/static/uploads/doctor-avatar-2.png" class="doctor-avatar" />
        <view class="doctor-info">
          <view class="name">在线医生</view>
          <view class="message">
            请描述你的疾病或症状、是否用药、就诊经历，需要我听过什么样的帮助
          </view>
          <view class="tips">
            <text class="iconfont icon-shield"></text>
            内容仅医生可见
          </view>
        </view>
      </view>
      <!-- 患者信息 -->
      <view class="patient-info">
        <view class="description">
          <uni-easyinput
            type="textarea"
            :input-border="false"
            v-model="illnessInfo.illnessDesc"
            placeholder-style="font-size: 30rpx; color: #979797"
            placeholder="请详细描述您的病情,病情描述不能为空"
          />
        </view>

        <view class="title">本次患病多久了？</view>
        <view class="tags">
          <text
            v-for="illnessTime in illnessTimes"
            :key="illnessTime.value"
            @click="onTimeTagClick(illnessTime.value)"
            v-model="illnessInfo.illnessTime"
            :class="{ active: illnessTime.value === illnessInfo.illnessTime }"
            class="tag"
          >
            {{ illnessTime.text }}
          </text>
        </view>

        <view class="title">此次病情是否去医院就诊过？</view>
        <view class="tags">
          <text
            v-for="consultFlag in consultFlags"
            :key="consultFlag.value"
            @click="onFlagTagClick(consultFlag.value)"
            :class="{ active: consultFlag.value === illnessInfo.consultFlag }"
            class="tag"
            v-model="illnessInfo.consultFlag"
            >{{ consultFlag.text }}</text
          >
        </view>
      </view>
      <!-- 上传图片 -->
      <view class="patient-picture">
        <uni-file-picker
          v-model="illnessInfo.pictures"
          @select="onSelect"
          :auto-upload="false"
          title="上传病情相关图片 (仅医生可见)"
          limit="8"
          :image-styles="{ width: '160rpx', height: '160rpx' }"
          file-extname="png,jpg,gif,webp"
        />
      </view>
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <button class="uni-button" @click="onNextStepClick" :disabled="!illnessInfo.illnessDesc">
        下一步
      </button>
    </view>
  </scroll-view>
</template>

<style lang="scss">
  @import './description.scss';
</style>
